package com.ruoyi.eam.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.*;
import com.ruoyi.eam.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.service.IEamChgHService;

/**
 * 资产变更Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-01
 */
@Service
public class EamChgHServiceImpl implements IEamChgHService
{
    @Autowired
    private EamChgHMapper eamChgHMapper;
    @Autowired
    private EamChgDMapper eamChgDMapper;
    @Autowired
    private EamTypeMapper typeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EamAssetMapper assetMapper;
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 查询资产变更
     *
     * @param alterationId 资产变更主键
     * @return 资产变更
     */
    @Override
    public EamChgH selectEamChgHByAlterationId(Long alterationId)
    {
        EamChgH chgH = eamChgHMapper.selectEamChgHByAlterationId(alterationId);
        EamChgD eamChgD = new EamChgD();
        eamChgD.setAlterationId(alterationId);
        List<EamChgD> chgDS = eamChgDMapper.selectEamChgDList(eamChgD);
        List<EamAsset> assets = new ArrayList<>();
        for (EamChgD chgD : chgDS) {
            EamAsset asset = assetMapper.selectEamAssetByAssetId(chgD.getAssetId());
            assets.add(asset);
        }
        chgH.setChgDS(assets);
        return chgH;
    }

    /**
     * 查询资产变更列表
     *
     * @param eamChgH 资产变更
     * @return 资产变更
     */
    @Override
    public List<EamChgH> selectEamChgHList(EamChgH eamChgH)
    {
        List<EamChgH> list = eamChgHMapper.selectEamChgHList(eamChgH);
        if(list.size() > 0){
            for (EamChgH chgH : list) {
                chgH.setTypeName(typeMapper.selectEamTypeByTypeId(chgH.getAssetType()).getTypeName());
                chgH.setPreparedName(userMapper.selectUserByUserId(chgH.getPrepared()).getNickName());
                if(chgH.getAuditPerson() != null){
                    chgH.setAuditName(userMapper.selectUserByUserId(chgH.getAuditPerson()).getNickName());
                    //获取审核人部门
                    chgH.setAuditDeptName(deptMapper.selectDeptByDeptId(userMapper.selectUserByUserId(chgH.getAuditPerson()).getDeptId()).getDeptName());
                }
                EamChgD eamChgD = new EamChgD();
                eamChgD.setAlterationId(chgH.getAlterationId());
                List<EamChgD> chgDS = eamChgDMapper.selectEamChgDList(eamChgD);
                User user = userMapper.selectUserByUserId(chgH.getPrepared());
                Dept dept = deptMapper.selectDeptByDeptId(user.getDeptId());
                chgH.setPreparedDeptName(dept.getDeptName());
                Dept dept1 = deptMapper.selectDeptByDeptId(chgH.getUseDept());
                chgH.setDeptName(dept1.getDeptName());
                List<EamAsset> assets = new ArrayList<>();
                for (EamChgD chgD : chgDS) {
                    EamAsset asset = assetMapper.selectEamAssetByAssetId(chgD.getAssetId());
                    assets.add(asset);
                }
                chgH.setChgDS(assets);
            }
        }
        return list;
    }

    /**
     * 新增资产变更
     *
     * @param eamChgH 资产变更
     * @return 结果
     */
    @Override
    public int insertEamChgH(EamChgH eamChgH)
    {
        //业务处理
        eamChgH.setCreateTime(DateUtils.getNowDate());
        eamChgH.setPreparedDate(DateUtils.getNowDate());
        //入库编码规则
        //获取当前时间
        String nowDate = DateUtils.getDateYYYYMMDD();
        //获取最后一条入库编码
        String chgCodeLast = eamChgHMapper.queryEamChgHList().get(eamChgHMapper.queryEamChgHList().size() - 1).getAlterationOdd();
        //截取流水线
        String serial =  chgCodeLast.substring(11,16);
        if(serial.equals("99999")){
            serial = "00001";
        }else{
            int num = Integer.parseInt(serial) + 1;
            serial = String.format("%05d",num);
        }
        //最终编码
        String chgCode = EamConstants.CHG_CODE_PR + nowDate + serial;
        eamChgH.setAlterationOdd(chgCode);
        eamChgH.setIsdel(1L);
        eamChgH.setReceiptsStatus(3L);
        return eamChgHMapper.insertEamChgH(eamChgH);
    }

    /**
     * 修改资产变更
     *
     * @param eamChgH 资产变更
     * @return 结果
     */
    @Override
    public int updateEamChgH(EamChgH eamChgH)
    {
        return eamChgHMapper.updateEamChgH(eamChgH);
    }

    /**
     * 批量删除资产变更
     *
     * @param alterationIds 需要删除的资产变更主键
     * @return 结果
     */
    @Override
    public int deleteEamChgHByAlterationIds(Long[] alterationIds)
    {
        return eamChgHMapper.deleteEamChgHByAlterationIds(alterationIds);
    }

    /**
     * 删除资产变更信息
     *
     * @param alterationId 资产变更主键
     * @return 结果
     */
    @Override
    public int deleteEamChgHByAlterationId(Long alterationId)
    {
        return eamChgHMapper.deleteEamChgHByAlterationId(alterationId);
    }

    @Override
    public int returnChgReceiptsStatus(EamChgH eamChgH) {
        return eamChgHMapper.returnChgReceiptsStatus(eamChgH);
    }
}
